// pages/ownerOertification/ownerOertification.js

var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:'',
    mobile:'',
    jobNo: '',
    listarea: [],
    listareaid: [],
    listareaindex: 0,
    areaId: '',
    Positive: '',
    unPositive: '',
    detailInfo: {
      pic1: null,
      pic2: null,
    },
    detail: {

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var userInfo = wx.getStorageSync("USER_INFO");
    var nickName = wx.getStorageSync("nickName");
    this.setData({
      nickName: nickName,
      mobile: userInfo.bindPhone,
    });
    this.loadAreas();
  },
  //选择
  changeUsedType: function (e) {
    
    this.data.areaId = this.data.listareaid[e.detail.value];
    this.data.listareaindex = e.detail.value;
    this.setData(this.data);
  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  bindKeyInput1: function (e) {
    this.data.detail[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  chooseImage: function (e) {

    var that = this;
    let pic = e.currentTarget.dataset.pic;
    
    wx.chooseImage({
      count: 1,
      success: function (res) {
        that.data.detailInfo[pic] = res.tempFiles[0];

        that.setData({
          detailInfo: that.data.detailInfo
        });
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //加载
  loadAreas() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/enterprises',

      success(res) {
        
        let listarea = [];
        listarea.push("请选择公司")
        let listareaid = [];
        listareaid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listarea.push(item.name)
            listareaid.push(item.id)
          }
          

          that.data.listarea = listarea;
          that.data.listareaid = listareaid;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  //获取信息
  getjobNo() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/parkStaffs/' + that.data.jobNo + '?parkId=' + that.data.areaId,
      success(res) {
        

        if (res.data.code == 0) {
          that.data.detail = res.data.data;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: res.data.message,
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  submit() {

    if (!this.data.areaId || this.data.areaId == 'null') {
      util.showModal('请选择公司');
      return
    }
    if (!this.data.nickName) {
      util.showModal('请选填写姓名');
      return
    }
    if (!this.data.mobile) {
      util.showModal('请选填写电话');
      return
    }
    var id = app.globalData.id || wx.getStorageSync("id");
    let that = this;
    var openId = app.globalData.openId || wx.getStorageSync("OPENID");
    var userInfo = app.globalData.userInfo || wx.getStorageSync("USER_INFO");
    wx.request({
      url: applet_park_api_url + '/api/enterpriseStaffs/applyCertificate',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      data: {
        enterpriseId: that.data.areaId ,
        nickName: that.data.nickName,
        phone: that.data.mobile,
        wxOpenId: openId

      },
      success(res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '提交成功',
          })

          userInfo.bindPhone = res.data.data.mobile;
          userInfo.nickName = res.data.data.nickName;
          wx.setStorageSync("USER_INFO", userInfo);
          wx.setStorageSync("nickName", res.data.data.nickName);
          wx.setStorageSync("id", res.data.data.id);
          wx.setStorageSync("userRoles", res.data.data.userRoles);
          
          app.globalData.userRoles = res.data.data.userRoles;//保存角色
          app.globalData.id = res.data.data.id;//保存用户id
          app.globalData.nickName = res.data.data.nickName;//保存用户昵称
          app.globalData.userInfo = res.data.data.userInfo;//保存用户信息
          // wx.switchTab({
          //   url: '../my/my',
          // })
          wx.navigateBack({
            
          })
        } else {
          // wx.showToast({
          //   title: '提交失败',
          // })
          util.showModal(res.data.message);
        }
      },
      fail() {

      }
    })


  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})